package com.charm.codes.nethard;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 *
 * @author tools
 * @date 2017/12/11
 */

public class RetrofitFactory {

    static List<Retrofit> retrofits = new ArrayList<Retrofit>();

    public static synchronized Retrofit build(String baseUrl){
        Iterator it = retrofits.iterator();
        while(it.hasNext()){
            Retrofit retrofit = (Retrofit) it.next();
            if (retrofit.baseUrl().equals(baseUrl)) {
                return retrofit;
            }
        }
        Retrofit retrofit = new Retrofit.Builder()
                .client(OkHttpFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .baseUrl(baseUrl)
                .build();
        retrofits.add(retrofit);
        return retrofit;
    }
}
